package com.woniu108.person.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu108.carService.commos.dto.AdminHistoryDto;
import com.woniu108.carService.commos.dto.AdminInfoDto;
import com.woniu108.carService.commos.dto.StoreDto;
import com.woniu108.carService.commos.exception.WoniuException;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.person.form.FilterHistoryForm;
import com.woniu108.person.model.AdminHistory;
import com.woniu108.person.model.AdminInfo;
import com.woniu108.person.model.AdminRole;
import com.woniu108.person.param.FilterAdminParam;
import com.woniu108.person.param.FilterHistoryParam;
import com.woniu108.person.service.AdminHistoryService;
import com.woniu108.person.service.AdminInfoService;
import com.woniu108.person.service.AdminRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 操作历史表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/adminHistory")
@Api(tags = "管理员操作历史接口")
public class AdminHistoryController {

    @Resource
    private AdminHistoryService adminHistoryService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private AdminInfoService adminInfoService;
    @Resource
    private AdminRoleService adminRoleService;
    public String getAdminId(String token){
        String refreshToken=redisUtil.getString(token);
        String adminId= JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        return adminId;
    }

    /**
     * 添加管理员操作历史记录
     * @param adminId 管理员编号
     * @param methodName 操作功能名称
     * @return result 是否正常访问接口结果
     */
    @GetMapping("/addAdminHistory.api")
    @ApiOperation("添加管理员操作历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId",value = "管理员编号"),
            @ApiImplicitParam(name = "methodName",value = "操作功能名称")
    })
    public Result addAdminHistory(String adminId,String methodName){
        AdminHistory history = new AdminHistory();
        history.setAdminId(adminId);
        history.setMethodName(methodName);
        history.setHistoryTime(LocalDateTime.now());
        adminHistoryService.save(history);
        return Result.getResult().setMsg("记录管理员操作历史成功");
    }

    /**
     * 展示管理员操作历史记录
     * @param token
     * @param form 管理员操作历史筛选条件
     * @return Page<AdminHistoryDto> 管理员操作历史记录信息集合
     */
    @PostMapping("/adminHistoryList.api")
    @ApiOperation("展示管理员操作历史记录")
    public Result<Page<AdminHistoryDto>> adminHistoryList(@RequestHeader String token,@RequestBody FilterHistoryForm form){
        String adminId1 = this.getAdminId(token);
        FilterHistoryParam param = BeanUtil.toBean(form, FilterHistoryParam.class);
        String adminName = param.getAdminName();
        String methodName = param.getMethodName();
        QueryWrapper<AdminInfo> wrapper=new QueryWrapper<>();
        QueryWrapper<AdminHistory> wrapper1 = new QueryWrapper<>();
        Integer adminRoleId = adminInfoService.getById(adminId1).getAdminRoleId();
        if(adminRoleId==2){
            //普通管理员账号仅可查看自身
            wrapper1.eq("admin_id",adminId1);
        }
        if(adminName!=null&&adminName!=""&&adminRoleId==1) {
            wrapper.like("admin_name",adminName);
        List<AdminInfo> list = adminInfoService.list(wrapper);
        int i=0;
        for (AdminInfo adminInfo : list) {
            String adminId = adminInfo.getAdminId();
            if(i>0){
                wrapper1.or().eq("admin_id",adminId);
            }else{
                wrapper1.eq("admin_id",adminId);
            }
            i=i+1;
        }
        }
        if(methodName!=null&&methodName!="")  wrapper1.like("method_name",methodName);
        //时间降序
        wrapper1.orderByDesc("history_time");
        List<AdminHistory> historyList = adminHistoryService.list(wrapper1);
        List<AdminHistoryDto> dtos=new ArrayList<>();
        int i=0;
        for (AdminHistory adminHistory : historyList) {
            if(i<param.getPageNum()*param.getPageSize().intValue()&&i>=(param.getPageNum()-1)*param.getPageSize().intValue()){
            AdminHistoryDto dto = BeanUtil.toBean(adminHistory, AdminHistoryDto.class);
            AdminInfo info = adminInfoService.getById(adminHistory.getAdminId());
            AdminRole adminRole = adminRoleService.getById(info.getAdminRoleId());
            dto.setAdminRoleName(adminRole.getAdminRoleName());
            dto.setAdminName(info.getAdminName());
            dtos.add(dto);
            }
        i=i+1;
        }

        //分页
        long total = historyList.size();
        long pages=total%param.getPageSize()==0?total/param.getPageSize():total/param.getPageSize()+1;
        Page<AdminHistoryDto> page=new Page<AdminHistoryDto>();
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        page.setRecords(dtos);
        page.setTotal(total);
        page.setPages(pages);
        return  Result.newPageInstance(AdminHistoryDto.class).setData(page);
    }
}

